package com.mobile.service.impl;

import com.mobile.service.dto.WorkOrderDTO;
import com.mobile.service.enums.WorkOrderEvent;
import com.mobile.service.enums.WorkOrderStatus;
import com.mobile.service.inter.WorkOrderService;
import com.mobile.service.inter.WorkOrderStateMachineService;
import com.mobile.service.statemachine.WorkOrderStateMachine;
import com.mobile.service.statemachine.WorkOrderStateContext;
import com.mobile.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 工单状态机服务实现
 */
@Slf4j
@Service("workOrderStateMachineService")
public class WorkOrderStateMachineServiceImpl implements WorkOrderStateMachineService {

    @Autowired
    private WorkOrderStateMachine stateMachine;
    
    @Autowired
    private WorkOrderService workOrderService;

    // 状态与可执行事件的映射
    private static final Map<WorkOrderStatus, Set<WorkOrderEvent>> AVAILABLE_EVENTS = new HashMap<>();
    
    static {
        AVAILABLE_EVENTS.put(WorkOrderStatus.SUBMITTED, Collections.singleton(WorkOrderEvent.SUBMIT_AUDIT));
        AVAILABLE_EVENTS.put(WorkOrderStatus.RE_DISPATCH, Collections.singleton(WorkOrderEvent.ENTER_AUDIT));
        
        Set<WorkOrderEvent> auditingEvents = new HashSet<>();
        auditingEvents.add(WorkOrderEvent.COMPLETE_AUDIT);
        auditingEvents.add(WorkOrderEvent.NEED_CORRECTION);
        auditingEvents.add(WorkOrderEvent.NEED_COLLABORATION);
        auditingEvents.add(WorkOrderEvent.REJECT_AUDIT);
        auditingEvents.add(WorkOrderEvent.NEED_REDISPATCH);
        AVAILABLE_EVENTS.put(WorkOrderStatus.AUDITING, Collections.unmodifiableSet(auditingEvents));
        
        AVAILABLE_EVENTS.put(WorkOrderStatus.NEED_CORRECT, Collections.singleton(WorkOrderEvent.COMPLETE_CORRECTION));
        
        Set<WorkOrderEvent> collaboratingEvents = new HashSet<>();
        collaboratingEvents.add(WorkOrderEvent.COMPLETE_COLLABORATION);
        collaboratingEvents.add(WorkOrderEvent.COLLABORATION_NEED_CORRECTION);
        AVAILABLE_EVENTS.put(WorkOrderStatus.COLLABORATING, Collections.unmodifiableSet(collaboratingEvents));
        
        AVAILABLE_EVENTS.put(WorkOrderStatus.COMPLETED, Collections.emptySet());
        AVAILABLE_EVENTS.put(WorkOrderStatus.REJECTED, Collections.emptySet());
    }

    @Override
    public Result<Boolean> executeTransition(Integer workOrderId, WorkOrderEvent event, WorkOrderStateContext context) {
        return stateMachine.executeTransition(workOrderId, event, context);
    }

    @Override
    public Set<WorkOrderEvent> getAvailableEvents(Integer workOrderId) {
        try {
            WorkOrderDTO workOrder = workOrderService.getWorkOrderById(workOrderId);
            if (workOrder == null) {
                return Collections.emptySet();
            }
            
            WorkOrderStatus currentStatus = WorkOrderStatus.fromCode(workOrder.getAllocationStatus());
            return AVAILABLE_EVENTS.getOrDefault(currentStatus, Collections.emptySet());
            
        } catch (Exception e) {
            log.error("获取可用事件失败: {}", workOrderId, e);
            return Collections.emptySet();
        }
    }

    @Override
    public Set<WorkOrderEvent> getAvailableEventsByStatus(String statusCode) {
        try {
            WorkOrderStatus status = WorkOrderStatus.fromCode(statusCode);
            return AVAILABLE_EVENTS.getOrDefault(status, Collections.emptySet());
        } catch (Exception e) {
            log.error("根据状态码获取可用事件失败: {}", statusCode, e);
            return Collections.emptySet();
        }
    }

    @Override
    public boolean isValidTransition(Integer workOrderId, WorkOrderEvent event) {
        Set<WorkOrderEvent> availableEvents = getAvailableEvents(workOrderId);
        return availableEvents.contains(event);
    }
}